iT邦幫忙

2021 iThome 鐵人賽

DAY 12
0
import shioaji as sj

api = sj.Shioaji()
accounts = api.login("YOUR_PERSON_ID", "YOUR_PASSWORD", contracts_timeout=10000)
api.activate_ca(
    ca_path="/c/your/ca/path/Sinopac.pfx",
    ca_passwd="YOUR_CA_PASSWORD",
    person_id="Person of this Ca",
)

contract = api.Contracts.Stocks.TSE.TSE0050
order = api.Order(
    price=90,
    quantity=10,
    action=sj.constant.Action.Buy,
    price_type=sj.constant.StockPriceType.LMT,
    order_type=sj.constant.TFTOrderType.ROD,     
    order_lot=sj.constant.TFTStockOrderLot.IntradayOdd, 
    account=api.stock_account,
)

trade = api.place_order(contract, order)
trade

輸出結果為

Trade(
    contract=Stock(
        exchange=<Exchange.TSE: 'TSE'>, 
        code='0050', 
        symbol='TSE0050', 
        name='元大台灣50', 
        category='00', 
        limit_up=115.8, 
        limit_down=94.8, 
        eference=105.3, 
        update_date='2020/09/21', 
        margin_trading_balance=15390, 
        short_selling_balance=2, 
        day_trade=<DayTrade.Yes: 'Yes'>
    ), 
    order=Order(
        action=<Action.Buy: 'Buy'>, 
        price=90.0, 
        quantity=10, 
        id='38e68afe', 
        seqno='482283', 
        ordno='WA313', 
        account=Account(
            account_type=<AccountType.Stock: 'S'>, 
            person_id='PAPIUSER04', 
            broker_id='9A95', 
            account_id='0506112', 
            signed=True
        ), 
        price_type=<StockPriceType.LMT: 'LMT'>, 
        order_type=<FuturesOrderType.ROD: 'ROD'>, 
        order_lot=<TFTStockOrderLot.IntradayOdd: 'IntradayOdd'>
    ), 
    status=OrderStatus(
        id='38e68afe', 
        status=<Status.Submitted: 'Submitted'>, 
        status_code='00', 
        order_datetime=datetime.datetime(2020, 9, 21, 14, 38, 51), deals=[]
    )
)

假如要抽單

api.update_status(api.stock_account)
api.cancel_order(trade)
api.update_status(api.stock_account)
trade
Trade(
    contract=Stock(
        exchange=<Exchange.TSE: 'TSE'>, 
        code='0050', 
        symbol='TSE0050', 
        name='元大台灣50', 
        category='00', 
        limit_up=115.8, 
        limit_down=94.8, 
        reference=105.3, 
        update_date='2020/09/21', 
        margin_trading_balance=15390, 
        short_selling_balance=2, 
        day_trade=<DayTrade.Yes: 'Yes'>
    ), 
    order=Order(
        action=<Action.Buy: 'Buy'>, 
        price=90.0, 
        quantity=10, 
        id='9b44c3b2', 
        seqno='482293', 
        ordno='WA328', 
        account=Account(
            account_type=<AccountType.Stock: 'S'>, 
            person_id='PAPIUSER04', 
            broker_id='9A95', 
            account_id='0506112', 
            signed=True
        ), 
        price_type=<StockPriceType.LMT: 'LMT'>, 
        order_type=<FuturesOrderType.ROD: 'ROD'>, 
        order_lot=<TFTStockOrderLot.IntradayOdd: 'IntradayOdd'>
    ), 
    status=OrderStatus(
        id='9b44c3b2', 
        status=<Status.Cancelled: 'Cancelled'>, 
        status_code='00', 
        order_datetime=datetime.datetime(2020, 9, 21, 14, 54, 36), 
        cancel_quantity=10, deals=[]
    )
)

改單,注意零股是不能修改價格的,只能調整買賣的股數

api.update_status(api.stock_account)
api.update_order(trade=trade, qty=2)
api.update_status(api.stock_account)
trade
Trade(
    contract=Stock(
        exchange=<Exchange.TSE: 'TSE'>, 
        code='0050', 
        symbol='TSE0050', 
        name='元大台灣50', 
        category='00', 
        limit_up=115.8, 
        limit_down=94.8, 
        reference=105.3, 
        update_date='2020/09/21', 
        margin_trading_balance=15390, 
        short_selling_balance=2, 
        day_trade=<DayTrade.Yes: 'Yes'>
    ), 
    order=Order(
        action=<Action.Buy: 'Buy'>, 
        price=90.0, 
        quantity=10, 
        id='9b44c3b2', 
        seqno='482293', 
        ordno='WA328', 
        account=Account(
            account_type=<AccountType.Stock: 'S'>, 
            person_id='PAPIUSER04', 
            broker_id='9A95', 
            account_id='0506112', 
            signed=True
        ), 
    price_type=<StockPriceType.LMT: 'LMT'>, 
    order_type=<FuturesOrderType.ROD: 'ROD'>, 
    order_lot=<TFTStockOrderLot.IntradayOdd: 'IntradayOdd'>), 
    status=OrderStatus(
        id='9b44c3b2', 
        status=<Status.Submitted: 'Submitted'>, 
        status_code='00', 
        order_datetime=datetime.datetime(2020, 9, 21, 14, 54, 36), 
        cancel_quantity=2, 
        deals=[]
    )
)

上一篇
買大買小!下好離手 - 下單
下一篇
開個六給他,讓他贏莊家一百塊 - 根據五檔報價之期貨買賣
系列文
在賭場尋求財富是否搞錯了什麼22
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言